package com.group525.cloudef.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group525.cloudef.Dao.user.LoginDao;
import com.group525.cloudef.common.CommonResult;
import com.group525.cloudef.entity.admin.User;
import com.group525.cloudef.entity.user.UUser;
import com.group525.cloudef.mapper.user.UUserMapper;
import com.group525.cloudef.service.user.UUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author group525
 * @since 2023-05-23
 */
@Service
public class UUserServiceImpl extends ServiceImpl<UUserMapper, UUser> implements UUserService {

    @Autowired
    UUserMapper userMapper;

    @Override
    public CommonResult<?> login(LoginDao loginDao) {
        //0.获取用户输入的username与password
        String phone = loginDao.getPhone();
        String password = loginDao.getPassword();
        //1.QueryWrapper进行筛选条件
        QueryWrapper<UUser> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("phone", phone)
                .eq("password", password);
        UUser uUser = userMapper.selectOne(queryWrapper);

        //2.创建Optional对象将User对象进行封装（对付空指针异常问题）
        Optional<UUser> userNameOptional = Optional.ofNullable(uUser);
        //2.1如果username存在
        if (userNameOptional.isPresent()) {
            return CommonResult.success("登录成功",uUser);

        } else {
            //2.2如果username不存在
            return CommonResult.failed("用户或密码不存在");
        }
    }


}
